<template>
  <div>
    <a-row :gutter="20">
      <a-col :span="8">
        <a-input-search
          placeholder="请输入内容"
          v-model="queryInfo.query"
          allowClear
          @clear="getGoodsList"
          enter-button
          @search="getSearchGoodsList"
        >
        </a-input-search>
        <a-button
          slot="append"
          icon="a-icon-search"
          @click="getGoodsList"
        ></a-button>
      </a-col>
      <a-col :span="4">
        <a-button type="primary" @click="goAddpage">添加商品</a-button>
      </a-col>
    </a-row>
    <br />

    <a-table
      align="center"
      rowKey="goods_id"
      :columns="columns"
      :data-source="goodslist"
      :pagination="pagination"
      bordered
      childrenColumnName
    >
      <!-- 时间 -->
      <template slot="addtime" slot-scope="add_time">
        {{ add_time | dateFormat }}
      </template>
      <!-- 操作列 -->
      <a-row slot="operation" slot-scope="dataIndex, record">
        <!-- 编辑按钮 -->
        <a-button
          type="primary"
          icon="edit"
          style="margin:3px"
          @click="showEditModal(record.goods_id)"
          >编辑
        </a-button>
        <!-- 删除按钮 -->
        <a-button
          type="danger"
          icon="delete"
          style="margin:3px"
          @click="removeById(record.goods_id)"
          >删除
        </a-button>
      </a-row>
    </a-table>

    <!-- 编辑对话框区域 -->
    <a-modal
      title="修改商品"
      :visible="editModalVisible"
      ok-text="确认"
      cancel-text="取消"
      labelAlign="left"
      @ok="editGoodsInfo"
      @cancel="editModalClosed"
    >
      <a-form-model
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <a-form-model-item label="商品ID" prop="goods_id">
          <a-input v-model="editForm.goods_id" disabled></a-input>
        </a-form-model-item>
        <a-form-model-item label="商品名称" prop="goods_name">
          <a-input v-model="editForm.goods_name"></a-input>
        </a-form-model-item>
        <a-form-model-item label="价格" prop="goods_price">
          <a-input v-model="editForm.goods_price"></a-input>
        </a-form-model-item>
        <a-form-model-item label="数量" prop="goods_number">
          <a-input v-model="editForm.goods_number"></a-input>
        </a-form-model-item>
        <a-form-model-item label="重量" prop="goods_weight">
          <a-input v-model="editForm.goods_weight"></a-input>
        </a-form-model-item>
        <a-form-model-item label="介绍" prop="goods_introduce">
          <quill-editor v-model="editForm.goods_introduce"></quill-editor>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
// import _ from 'lodash'
export default {
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表
      goodslist: [],
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`
        },
        {
          title: '商品名称',
          dataIndex: 'goods_name'
        },
        {
          title: '商品价格(元)',
          dataIndex: 'goods_price'
        },
        {
          title: '商品重量',
          dataIndex: 'goods_weight'
        },
        {
          title: '创建时间',
          dataIndex: 'add_time',
          scopedSlots: { customRender: 'addtime' }
        },
        {
          title: '操作',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      pagination: {
        position: 'bottom',
        // 总数据条数
        total: 0,
        current: 1,
        pageSize: 5,
        showSizeChanger: true,
        pageSizeOptions: ['1', '2', '5', '10', '20'],
        showTotal: total => `总共 ${total} 条`,
        onChange: (current, pageSize) => this.pageChange(current, pageSize),
        onShowSizeChange: (current, size) => this.showSizeChange(current, size)
      },
      editModalVisible: false,
      // 编辑框内容
      editForm: {},
      // 编辑框规则
      editFormRules: {
        goods_id: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ],
        goods_name: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 根据分页获取对应的商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败！')
      }
      // this.$message.success('获取商品列表成功！')
      // console.log(res.data)
      this.goodslist = res.data.goods
      this.pagination.total = res.data.total
      this.pagination.current = this.queryInfo.pagenum
      this.pagination.pageSize = this.queryInfo.pagesize
    },
    // 搜索
    getSearchGoodsList() {
      this.queryInfo.pagenum = 1
      this.getGoodsList()
    },
    // 监听 pagesize 改变
    showSizeChange(current, size) {
      this.queryInfo.pagesize = size
      this.pagination.pageSize = size
      this.getGoodsList()
    },
    // 监听 pagenum 改变
    pageChange(current, pageSize) {
      this.queryInfo.pagenum = current
      this.getGoodsList()
    },
    // 打开编辑框
    async showEditModal(id) {
      // 查询当前参数的信息
      const { data: res } = await this.$http.get(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取参数信息失败！')
      }
      this.editForm = res.data
      console.log(this.editForm)
      this.editModalVisible = true
    },
    // 提交编辑框
    editGoodsInfo() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写必要的表单项！')
        }
        const { data: res } = await this.$http.put(
          `goods/${this.editForm.goods_id}`,
          this.editForm
        )
        console.log({
          goods_name: this.editForm.goods_name,
          goods_price: this.editForm.goods_price,
          goods_number: this.editForm.goods_number,
          goods_weight: this.editForm.goods_weight,
          goods_introduce: this.editForm.goods_introduce,
          pics: this.editForm.pics,
          attrs: this.editForm.attrs
        })
        if (res.meta.status !== 201) {
          return this.$message.error('修改参数失败！')
        }
        this.$message.success('修改参数成功！')
        this.getGoodsList()
        this.editModalVisible = false
      })
    },
    // 关闭编辑框
    editModalClosed() {
      this.$refs.editFormRef.resetFields()
      this.editModalVisible = false
    },
    async removeById(id) {
      const that = this
      this.$confirm({
        content: '此操作将永久删除该商品, 是否继续?',
        cancelText: '取消',
        okText: '确定',
        async onOk() {
          // 删除的业务逻辑
          const { data: res } = await that.$http
            .delete(`goods/${id}`)
            .catch(err => err)
          if (res.meta.status !== 200) {
            return that.$message.error('删除商品失败！')
          }
          that.$message.success('删除商品成功！')
          that.getGoodsList()
        },
        onCancel() {
          that.$message.info('已取消删除！')
        }
      })
    },
    goAddpage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style></style>
